package com.example.mvvm.base.data;

import androidx.annotation.Nullable;
import androidx.lifecycle.MutableLiveData;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;

import javax.security.auth.Destroyable;

public class SafeMutableLiveListData<T> extends MutableLiveData<Vector<T>> {

    public static class Builder<T> {
        public SafeMutableLiveListData<T> mutableLiveListData;

        public Builder(SafeMutableLiveListData<T> mutableLiveListData) {
            this.mutableLiveListData = mutableLiveListData;
        }

        public Vector <T> entity;

        public Builder add(T data) {
            if (entity == null) {
                entity = new Vector <T>();
            }
            entity.add(data);
            return this;
        }

        public void buildPost() {
            mutableLiveListData.postValue(entity);
        }

        public void buildAdd() {
            mutableLiveListData.addData(entity);
        }
    }

    public void addData(T data) {
        Vector <T> value = this.getValue();
        if (value == null) {
            value = new Vector <T>();
        }
        value.add(data);
        this.setValue(value);
    }

    @Override
    public void setValue(Vector<T> value) {
        super.setValue(value);
    }

    public void addData(List<T> list) {
        if (list != null && list.size() > 0) {
            Vector <T> value = getValue();
            value.addAll(list);
            setValue(value);
        }
    }

    public void addData(T... data) {
        Vector <T> value = this.getValue();
        if (value == null) {
            value = new Vector <T>();
        }
        for (T datum : data) {
            value.add(datum);
        }
        this.setValue(value);
    }

    public void setData(int index, T data) {
        Vector <T> value = this.getValue();
        if (value != null) {
            value.set(index, data);
            this.setValue(value);
        }
    }

    public T getData(T data) {
        List<T> value = this.getValue();
        if (value != null) {
            return value.get(value.indexOf(data));
        }
        return null;
    }

    public void indexOf(T data) {
        Vector <T> value = this.getValue();
        if (value != null) {
            if (value.contains(data)) {
                value.set(value.indexOf(data), data);
            } else {
                addData(data);
            }
        } else {
            addData(data);
        }
        this.setValue(value);
    }

    public void deleteList(T data) {
        if (data != null) {
            Vector <T> value = this.getValue();
            if (value != null) {
                value.remove(data);
                this.setValue(value);
            }
        }
    }

    public void clean() {
        Vector <T> value = this.getValue();
        if (value != null) {
            value.clear();
            this.setValue(value);
        }
    }


    @Nullable
    @Override
    public Vector <T> getValue() {
        Vector <T> value = super.getValue();
        if (value == null) {
            value = new Vector <>();
        }
        return value;
    }

    public void callValue(){
        postValue(getValue());
    }


}
